package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.ReviewGroupInfoMapper;
import com.px.dal.SpecialInfoMapper;
import com.px.model.ReviewGroupInfo;
import com.px.model.SpecialistReviewRel;
import com.px.model.SpeciallistInfo;
import com.px.model.getData;
import com.px.unit.Response;

@Controller
public class ReviewGroupInfoController {

	ReviewGroupInfoMapper reviewtb_mapper = new ReviewGroupInfoMapper();
	SpecialInfoMapper special_mapper = new SpecialInfoMapper();

	/**
	 * 获取所有项目分类信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getAllReviewGroupInfos", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getAllReviewGroupInfos() {
		// 示例化一个自定义对象，用来传递我们需要的参数
		getData mydata = new getData();
		List<ReviewGroupInfo> reviewGroupInfoList = reviewtb_mapper.getAllReviewGroupInfos();
		mydata.setReviewGroupInfoList(reviewGroupInfoList);
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 获取指定Id项目信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getReviewGroupByReviewGroupId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getReviewProjectByReviewProjectId(@RequestBody ReviewGroupInfo reviews) {
		if (reviews != null) {
			// 判断查询的评审组是否存在
			int checkid = reviewtb_mapper.checkReviewIdIsExist(reviews);
			if (!(checkid > 0)) {
				return new Response(304, "该评审组不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<ReviewGroupInfo> reviewGroupInfoList = reviewtb_mapper.geyReviewGroupByReviewGroupId(reviews);
				mydata.setReviewGroupInfoList(reviewGroupInfoList);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");
	}

	/**
	 * 添加会员单位负责人信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/insertReviewGroups", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response insertReviewGroups(@RequestBody ReviewGroupInfo reviews) {

		if (reviews != null) {

			// 判断添加的会员单位负责人账户不能重复
			int checkname = reviewtb_mapper.checkReviewIdIsExist(reviews);
			if (checkname > 0) {
				return new Response(304, "已存在同名帐号");
			} else {
				int counts = reviewtb_mapper.insertReviewGroups(reviews);
				if (counts > 0) {
					return new Response(200, "添加成功！");
				} else {
					return new Response(304, "添加失败！");
				}
			}
		}
		return new Response(304, "添加失败！");
	}

	/**
	 * 根据评审组ID删除评审组信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteReviewGroupInfoByReviewGroupInfoId", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteReviewGroupInfoByReviewGroupInfoId(@RequestBody ReviewGroupInfo reviews) {

		if (reviews != null) {

			// 判断删除的评审组是否存在
			int checkid = reviewtb_mapper.checkReviewIdIsExist(reviews);
			if (!(checkid > 0)) {
				return new Response(304, "该评审组不存在");
			} else {
				int counts = reviewtb_mapper.deleteReviewGroupInfoByReviewGroupInfoId(reviews);
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 根据评审组ID修改评审组信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateReviewGroupByReviewGroupId", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateReviewGroupByReviewGroupId(@RequestBody ReviewGroupInfo reviews) {

		if (reviews != null) {

			// 判断修改的评审组是否存在
			int checkid = reviewtb_mapper.checkReviewIdIsExist(reviews);
			if (!(checkid > 0)) {
				return new Response(304, "该评审组不存在");
			} else {
				int counts = reviewtb_mapper.updateReviewGroupByReviewGroupId(reviews);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

	/**
	 * 根据专家id查找评审组信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getReviewGroupInfoBySpecialistId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getReviewGroupInfoBySpecialistId(@RequestBody SpecialistReviewRel specialistReviewRel) {
		if (specialistReviewRel != null) {
			SpeciallistInfo speciallistInfo = new SpeciallistInfo();
			speciallistInfo.setSpecialist_id(specialistReviewRel.getSpecialist_id());
			// 判断查询的评审组是否存在
			int checkid = special_mapper.checkSpecialIdIsExist(speciallistInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该专家不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<ReviewGroupInfo> reviewGroupInfoList = reviewtb_mapper
						.getReviewGroupInfoBySpecialistId(specialistReviewRel.getSpecialist_id());
				mydata.setReviewGroupInfoList(reviewGroupInfoList);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");
	}

}
